﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      CIO Role Assignment
// Created By:     Parker Zhou (parkerz@wicresoft.com)
// Created Date:   2011-1-27
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASB.CC.Business.CIS;
using ASBCC.Business;
using ASBCC.Business.CIS;
using ASBCC.Database.EntityFramework;

namespace BEGDC.CC.Web.CIS2.Overseas.Admin
{
    public partial class RoleAssignment : EntityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }


        protected bool IsAdmin(object dataItem)
        {
            var employee = dataItem as Employee;
            return employee.Roles.Any(p => !p.Disabled && p.IsAdmin && p.BelongsTo == "CIO");
        }

        protected IEnumerable GetRoles(object dataItem)
        {
            var employee = dataItem as Employee;
            return employee.Roles.Where(p => !p.Disabled && p.BelongsTo == "CIO" && p.Description != "+country+").OrderBy(p => p.IsAdmin);
        }


        protected override EntityDataSource EntitySource
        {
            get { return edsMain; }
        }

        protected override GridView EntityGrid
        {
            get { return gvMain; }
        }

        protected override DetailsView EntityDetails
        {
            get { return dvMain; }
        }

        protected override TextBox EntitySearchTxt
        {
            get { return txtSearch; }
        }

        protected override Button EntityAddBtn
        {
            get { return btnAdd; }
        }

        protected override Button EntityClearBtn
        {
            get { return btnClear; }
        }

        protected override Button EntitySearchBtn
        {
            get { return btnSearch; }
        }

        protected override void OnEntityDisabling(int entityKey)
        {
            // do nothing
        }

        protected override void OnEntityInserting(EntityDataSourceChangingEventArgs e)
        {
            e.Cancel = true;
            UpdateEmployee(e);
            var employee = e.Entity as Employee;
            var employeeNO = RoleAssignmentHelper.getEmployeeNo(employee.NameEN);
            var isAdmin = LogInfoHelper.GetIsAdminO(employeeNO);
            var roles = LogInfoHelper.GetRolesO(employeeNO);
            string country = this.Request.Form["country-select"];
            var countries = LogInfoHelper.GetCountryO(employee.NameEN, country);
            GlobalHelper.LogOperation(this.User.Identity.Name, SystemModule.CIO, "RoleManagement inserted. EmployeeNo=" + employeeNO + "; CIL=" + employee.NameEN +
             "; Roles=" + roles + "; IsAdmin=" + isAdmin + countries);
            Entity_Changed(null, null);
        }

        protected override void OnEntityUpdating(EntityDataSourceChangingEventArgs e)
        {
            e.Cancel = true;
            UpdateEmployee(e);
            var employee = e.Entity as Employee;
            var isAdmin = LogInfoHelper.GetIsAdminO(employee.EmployeeNo);
            var roles = LogInfoHelper.GetRolesO(employee.EmployeeNo);
            string country = this.Request.Form["country-select"];
            var countries = LogInfoHelper.GetCountryO(employee.NameEN, country);
            GlobalHelper.LogOperation(this.User.Identity.Name, SystemModule.CIO, "RoleManagement updated. EmployeeNo=" + employee.EmployeeNo + "; CIL=" + employee.NameEN +
             "; Roles=" + roles + "; IsAdmin=" + isAdmin + countries);
            Entity_Changed(null, null);
        }

        protected override string GetEntityWhereExpression(string keyword)
        {
           
            var filter = "";

            if (!string.IsNullOrEmpty(keyword))
            {
                filter += "((it.NameEN like N'%" + keyword + "%') ";
                filter += " or Count(select value r.RoleID from it.Roles as r where !r.Disabled and r.BelongsTo=='CIO' and (r.Name like\"%" + keyword + "%\" or r.BelongsTo like\"%" + keyword + "%\"))>0) and ";
            }
            filter += "(Count(select value r.RoleID from it.Roles as r where !r.Disabled and r.BelongsTo == 'CIO'" + (this.admin.Checked ? " and (r.IsAdmin)" : "") + ") > 0 )";
            return filter;
        }

        protected override Panel GridPanel
        {
            get
            {
                return gridPanel;
            }
        }

        private void UpdateEmployee(EntityDataSourceChangingEventArgs e)
        {
            var user = e.Entity as Employee;
            string emNo = user.NameEN;
            RoleAssignmentHelper arh = new RoleAssignmentHelper();
            bool isAdmin = this.Request.Form["isadmin"] == "on";

            string country = this.Request.Form["country-select"];
            string region = this.Request.Form["region-select"];

            if (!RoleAssignmentHelper.UpdateUserOver(isAdmin, country, region, emNo))
                ClientScript.RegisterClientScriptBlock(GetType(), "Error", "<script>alert('Wrong CIL.')</script>");

        }

        /// <summary>
        /// 通过IsAdmin取yes/NO
        /// </summary>
        protected string GetIsAdmin(object isAdmin)
        {
            if (isAdmin == null)
                return "";
            if (Convert.ToBoolean(isAdmin) == true)
                return "Yes";
            if (Convert.ToBoolean(isAdmin) == false)
                return "No";
            return "";
        }

        protected string GetCountrySelect()
        {
            return "\"{" + RoleAssignmentHelper.GetCountrySelectString() + "}\"";
        }

        protected string GetRegionSelect()
        {
            return "\"{" + RoleAssignmentHelper.GetRegionSelectString() + "}\"";
        }

        [WebMethod]
        public static string GetRoleData(string cil)
        {
            if (!String.IsNullOrEmpty(cil))
                return RoleAssignmentHelper.GetUserRoleOver(cil);
            return "";
        }
        protected void admin_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
